import 'dart:convert';

import 'package:app_flutter/routes.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

import 'area/page.dart';
import 'car/page.dart';
import 'company/page.dart';
import 'safer/page.dart';
import 'teacher/page.dart';
import 'tester/page.dart';

class PageHost extends StatelessWidget {
  final hostTitle;
  final pageType;

  PageHost({String hostTitle, String pageType})
      : this.hostTitle = hostTitle,
        this.pageType = pageType;

  @override
  Widget build(BuildContext context) {
//    var list = List<int>();
//    jsonDecode(hostTitle).forEach(list.add);
//    String value = Utf8Decoder().convert(list);

    //初始化标签
    List<Widget> tabs = [
      renderTab(Text('全部')),
      renderTab(Text("待审核")),
      renderTab(Text("审核通过"))
    ];
    return TabBarWidget(
      initPage: 1,
      hostTitle: hostTitle,
      type: TabBarWidget.TOP_TAB,
      tabItems: tabs,
      tabViews: _setPages(pageType),
      indicatorColor: Color(0xff23AFDC),
    );
  }

  _setPages(String pageType) {
    int index = int.parse(pageType);
    switch (index) {
      case 0:
        return [ComPage(type: ''), ComPage(type: '2'), ComPage(type: '1')];
        break;
      case 1:
        return [AreaPage(), AreaPage(), AreaPage()];
        break;
      case 2:
        return [TeacherPage(), TeacherPage(), TeacherPage()];
        break;
      case 3:
        return [CarPage(), CarPage(), CarPage()];
        break;
      case 4:
        return [SaferPage(), SaferPage(), SaferPage()];
        break;
      case 5:
        return [TesterPage(), TesterPage(), TesterPage()];
        break;
    }
  }

  renderTab(text) {
    //返回一个标签
    return Tab(
        child: Container(
      //设置paddingTop为6
      padding: EdgeInsets.only(top: 6),
      //一个列控件
      child: Column(
        //竖直方向居中
        mainAxisAlignment: MainAxisAlignment.center,
        //水平方向居中
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[text],
      ),
    ));
  }
}

class TabBarWidget extends StatefulWidget {
  static const int BOTTOM_TAB = 1;
  static const int TOP_TAB = 2;
  final int type;
  final String hostTitle;
  final List<Widget> tabItems;
  final List<Widget> tabViews;
  final Color backgroundColor;
  final Color indicatorColor;
  final Widget title;
  final int initPage;
  final ValueChanged<int> onPageChanged;

  TabBarWidget(
      {Key key,
      this.hostTitle,
      this.type,
      this.tabItems,
      this.tabViews,
      this.backgroundColor,
      this.indicatorColor,
      this.title,
      this.initPage,
      this.onPageChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TabBarState(this.hostTitle, this.type, this.tabViews,
        this.indicatorColor, this.title, this.initPage, this.onPageChanged);
  }
}

class _TabBarState extends State<TabBarWidget>
    with SingleTickerProviderStateMixin {
  final String _hostTitle;
  final int _type;
  final int _initPage;
  final List<Widget> _tabViews;
  final Color _indicatorColor;
  final Widget _title;
  final ValueChanged<int> _onPageChanged;

  _TabBarState(this._hostTitle, this._type, this._tabViews,
      this._indicatorColor, this._title, this._initPage, this._onPageChanged)
      : super();
  TabController _tabController;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
        initialIndex: _initPage, length: widget.tabItems.length, vsync: this);
    _pageController = PageController(initialPage: _initPage, keepPage: true);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var list = List<int>();
    jsonDecode(_hostTitle).forEach(list.add);
    String value = Utf8Decoder().convert(list);
    if (this._type == TabBarWidget.TOP_TAB) {
      return Scaffold(
        appBar: AppBar(
            backgroundColor: Color(0xff23AFDC),
            elevation: 0,
            title: Text(value),
            centerTitle: true,
            actions: <Widget>[
              IconButton(
                  icon: Icon(
                    Icons.search,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    String route = '${Routes.comSearch}';
                    Routes.router.navigateTo(context, route,
                        replace: false, transition: TransitionType.fadeIn);
                  }),
            ],
            leading: IconButton(
                icon: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                ),
                onPressed: () {
                  Navigator.pop(context);
                })),
        body: Container(
          color: Colors.white,
          child: Column(
            children: <Widget>[
              TabBar(
                tabs: widget.tabItems,
                controller: _tabController,
                indicatorColor: _indicatorColor,
                indicatorSize: TabBarIndicatorSize.label,
                labelColor: Color(0xff23AFDC),
                unselectedLabelColor: Color(0xff999999),
                onTap: (index) {
                  _pageController.jumpToPage(index);
                },
              ),
              Expanded(
                child: PageView(
                  controller: _pageController,
                  children: _tabViews,
                  onPageChanged: (index) {
                    _tabController.animateTo(index);
                    _onPageChanged?.call(index);
                  },
                ),
              ),
            ],
          ),
        ),
      );
    }
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: _title,
      ),
      body: TabBarView(
        children: _tabViews,
        controller: _tabController,
      ),
      bottomNavigationBar: Material(
        color: Colors.red,
        child: TabBar(
          tabs: widget.tabItems,
          indicatorColor: _indicatorColor,
          controller: _tabController,
        ),
      ),
    );
  }
}
